package it.univaq.mwt.facade;

import java.io.IOException;
import java.util.List;
import javax.persistence.PersistenceException;
import it.univaq.mwt.business.NewsService;
import it.univaq.mwt.exceptionmonitor.LivelloEx;
import it.univaq.mwt.exceptionmonitor.WishListrException;
import it.univaq.mwt.exceptionmonitor.WishlistUserException;
import it.univaq.mwt.exceptionmonitor.WishlistrMessage;
import it.univaq.mwt.log.LivelloLog;
import it.univaq.mwt.log.LogManager;
import it.univaq.mwt.model.bean.NewsBeanRemote;
import it.univaq.mwt.model.entity.News;

public class FacadeNews implements NewsService,LivelloEx, LivelloLog {

	private NewsBeanRemote nbr;

	public FacadeNews(NewsBeanRemote nbr) {
		super();
		this.nbr = nbr;
	}

	
	public void addNews(News n) throws WishlistUserException {
		try{
			nbr.setNews(n);
			LogManager.getIstance().scrivi("FacadeNews - addNews: Aggiunta la news con id " + n.getId(), RUN);

		} catch (IOException e) {
			new WishListrException(e,ERROR);
		} catch (PersistenceException e) {
			System.out.println("errore di persistenza");
		} catch (Exception e) {
			System.out.println("errore sql");
		}
	}

	
	public void deleteNews(int id) throws WishlistUserException {
		
		try{
			try {
				nbr.delete(id);
			} catch (WishlistrMessage e) {
				LogManager.getIstance().scrivi("FacadeNews - deleteNews " + e.getMessage(), ERR);
				//throw
			}
			
			LogManager.getIstance().scrivi("FacadeNews - deleteNews : Cancellazione della news con id " + id, RUN);
		} catch (IOException e) {
			new WishListrException(e,ERROR);
		}
	}

	
	public void editNews(News n) throws WishlistUserException {

		try{
			try {
				nbr.editNews(n);
			} catch (WishlistrMessage e) {
				LogManager.getIstance().scrivi("FacadeNews - editNews " + e.getMessage(), ERR);
			}

			LogManager.getIstance().scrivi("FacadeNews - editNews: Modifica della news con id " + n.getId(), RUN);

		} catch (IOException e) {
			new WishListrException(e,ERROR);
		}
	}

	
	public List<News> getList(int i, int j) {
		List<News> list = null;
		
		try {
			try {
				list = nbr.getList(i, j);
			} catch (WishlistrMessage e) {
				LogManager.getIstance().scrivi("FacadeNews - getList : " + e.getStackTrace(), ERR);
				// throws
			}
		
			LogManager.getIstance().scrivi("FacadeNews - getList : Elenco delle news da " + i + " fino a " + (i + j), RUN);
		} catch (IOException e) {
			new WishListrException(e,ERROR);
		}		
		return list;
	}

	
	public News getNews(int id) {

		News news = null;
		try{		
			try {
				news = nbr.getNews(id);
			} catch (WishlistrMessage e) {
				LogManager.getIstance().scrivi("FacadeNews - getList : " + e.getStackTrace(), ERR);
				// 	throws
			}
	
			LogManager.getIstance().scrivi("FacadeNews - getNews: Richiesta della news con id " + id, RUN);
		} catch (IOException e) {
			new WishListrException(e,ERROR);
		}	
		
		return news;
	}

	
	public void delete(int id) throws WishlistUserException {
		try {
			try {
				nbr.delete(id);
			} catch (WishlistrMessage e) {
				LogManager.getIstance().scrivi("FacadeNews - deleteNews " + e.getMessage(), ERR);
			}

			LogManager.getIstance().scrivi("FacadeNews - delete: Cancellazione della news con id " + id, RUN);
		} catch (IOException e) {
			new WishListrException(e,ERROR);
		}	
	}

	
	public int getNewsNumber() {
		
		int n = 0;

		try{
			try {
				n = nbr.getNewsNumber();
			} catch (WishlistrMessage e) {
				LogManager.getIstance().scrivi("FacadeNews - getNewsNumber : " + e.getStackTrace(), ERR);
				// 	throws
			}
			LogManager.getIstance().scrivi("FacadeNews - getNewsNumber : Numero totale delle news " + n, RUN);
		} catch (IOException e) {
			new WishListrException(e,ERROR);
		}	
		return n;
	}

	public List<News> getNewsListsmall(int limit) {

		List<News> news = null;
		
		try {
			try {
				news = nbr.getNewsListSmall(limit);
			} catch (WishlistrMessage e) {
				LogManager.getIstance().scrivi("FacadeNews - getNewsListsmall : " + e.getStackTrace(), ERR);
				// 	throws
			}

			LogManager.getIstance().scrivi("FacadeNews - getNewsListsmall : elenco News limit " + limit, RUN);
		} catch (IOException e) {
			new WishListrException(e,ERROR);
		}	
		return news;
	}
}